# 6、贪心 &排序不等式
# AcWing 913. 排队打水
import java.util.*;
import java.util.concurrent.LinkedTransferQueue;
//ACWing
public class Main {
public static void main(String[] args) {
Main main = new Main();
main.init();
}
// 构造大顶堆:
PriorityQueue<Integer> small_heap
= new PriorityQueue<>();
void init() {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
for (int i = 0; i < n; i++) {
small_heap.offer(sc.nextInt());
}
long res=0;
while (small_heap.size()>0){
long a=small_heap.poll();
res+=a*small_heap.size();
}
System.out.println(res);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
← 6、贪心 &哈夫曼树 6、贪心 &推公式 →